﻿using Microsoft.Win32;
using System;
using System.Drawing;
using System.IO;
using System.Management;
using System.Net;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace XuanFe.AppLaunch.Common
{
    /// <summary>
    /// 公共函数
    /// </summary>
    public class Global
    {
        private static Global instance = new Global();

        public static Global Instance
        {
            get
            {
                return instance;
            }
        }
        /// <summary>
        /// 检查字段是否为空，TextBox，ComboBox
        /// </summary>
        /// <param name="ControlName">检查的控件变量</param>
        /// <param name="EmptyFlg">是否允许为空</param>
        /// <param name="Message">输出的错误消息</param>
        /// <returns>为空或不符合条件返回true，否则返回false</returns>
        public bool MandatoryFieldEmpty(Control ControlName, bool EmptyFlg, ref string Message)
        {
            string msg1 = "该字段不能为空！";
            string msg2 = "该字段已经超过了预设的长度（{0}个字符，{1}个汉字）！";

            switch (ControlName.GetType().Name)
            {
                case "MemoEdit":
                    DevExpress.XtraEditors.MemoEdit me = (DevExpress.XtraEditors.MemoEdit)ControlName;
                    if (!EmptyFlg && string.IsNullOrEmpty(Convert.ToString(me.EditValue)))
                    {
                        Message = msg1;
                        return (true);
                    }
                    else if (me.Properties.MaxLength > 0 && StrLength(Convert.ToString(me.EditValue)) > me.Properties.MaxLength)
                    {
                        Message = string.Format(msg2, me.Properties.MaxLength, me.Properties.MaxLength / 2);
                        return (true);
                    }
                    break;
                case "TextEdit":
                    DevExpress.XtraEditors.TextEdit te = (DevExpress.XtraEditors.TextEdit)ControlName;
                    if (!EmptyFlg && string.IsNullOrEmpty(Convert.ToString(te.EditValue)))
                    {
                        Message = msg1;
                        return (true);
                    }
                    else if (te.Properties.MaxLength > 0 && StrLength(Convert.ToString(te.EditValue)) > te.Properties.MaxLength)
                    {
                        Message = string.Format(msg2, te.Properties.MaxLength, te.Properties.MaxLength / 2);
                        return (true);
                    }
                    break;
                case "ComboBoxEdit":
                    DevExpress.XtraEditors.ComboBoxEdit ce = (DevExpress.XtraEditors.ComboBoxEdit)ControlName;
                    if (!EmptyFlg && (string.IsNullOrEmpty(Convert.ToString(ce.EditValue)) || ce.SelectedIndex == -1))
                    {
                        Message = msg1;
                        return (true);
                    }
                    break;
                case "TextBox":
                    TextBox t = (TextBox)ControlName;
                    if (!EmptyFlg && string.IsNullOrEmpty(t.Text))
                    {
                        Message = msg1;
                        return (true);
                    }
                    else if (StrLength(t.Text) > t.MaxLength)
                    {
                        Message = string.Format(msg2, t.MaxLength, t.MaxLength / 2);
                        return (true);
                    }
                    break;
                case "ComboBox":
                    ComboBox c = (ComboBox)ControlName;
                    if (!EmptyFlg && (string.IsNullOrEmpty(c.Text) || c.SelectedIndex == -1))
                    {
                        Message = msg1;
                        return (true);
                    }
                    break;
                case "SearchLookUpEdit":
                    DevExpress.XtraEditors.SearchLookUpEdit s = (DevExpress.XtraEditors.SearchLookUpEdit)ControlName;
                    if (!EmptyFlg && string.IsNullOrEmpty(s.Text))
                    {
                        Message = msg1;
                        return (true);
                    }
                    break;
                case "DateEdit":
                    DevExpress.XtraEditors.DateEdit de = (DevExpress.XtraEditors.DateEdit)ControlName;
                    if (!EmptyFlg && de.EditValue == null)
                    {
                        Message = msg1;
                        return (true);
                    }
                    break;
            }

            return (false);
        }

        #region Win API

        [StructLayout(LayoutKind.Sequential)]
        private struct SystemTime
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMiliseconds;
        }
        /// <summary>
        /// 设置本地时间
        /// </summary>
        /// <param name="sysTime"></param>
        /// <returns></returns>
        [DllImport("Kernel32.dll")]
        private static extern bool SetLocalTime(ref SystemTime sysTime);
        /// <summary>
        /// 得当前本地时间
        /// </summary>
        /// <param name="sysTime"></param>
        [DllImport("Kernel32.dll")]
        private static extern void GetLocalTime(ref SystemTime sysTime);

        #endregion Win API

        /*---------------------------------------------------
         * 转换函数
         *---------------------------------------------------*/

        /// <summary>
        /// 字节数组转换为图片
        /// </summary>
        /// <param name="b">字节数组</param>
        /// <returns></returns>
        public Image BytesToImage(Byte[] b)
        {
            return (new Bitmap(new MemoryStream(b)));
        }

        /// <summary>
        /// 图片转换为字节数组
        /// </summary>
        /// <param name="image">图片</param>
        /// <returns></returns>
        public Byte[] ImageToBytes(Image image)
        {
            using (MemoryStream memStream = new MemoryStream())
            {
                image.Save(memStream, image.RawFormat);
                return (memStream.GetBuffer());
            }
        }

        /*---------------------------------------------------
         * 扩展的文件目录操作函数
         *---------------------------------------------------*/

        /// <summary>
        /// 应用程序的路径
        /// </summary>
        /// <returns>返回应用程序的所在目录路径</returns>
        public string AppPath()
        {
            return (Path.GetDirectoryName(Application.ExecutablePath));
        }

        /// <summary>
        /// 路径后面加"\"修饰
        /// </summary>
        /// <param name="Path">目标目录路径</param>
        /// <returns>返回加"\"修饰的目录路径</returns>
        public string MakePath(string Path)
        {
            if (Path.Substring(Path.Length - 1, 1).Equals("\\"))
                return (Path);
            return (string.Concat(Path, "\\"));
        }

        /*---------------------------------------------------
         * 系统功能函数
         *---------------------------------------------------*/

        /// <summary>
        /// 显示等待光标
        /// </summary>
        public void BeginWait()
        {
            Cursor.Current = Cursors.WaitCursor;
        }

        /// <summary>
        /// 结束等待光标
        /// </summary>
        public void EndWait()
        {
            Cursor.Current = Cursors.Default;
        }

        /// <summary>
        /// 获取本机名
        /// </summary>
        /// <returns>返回本地计算机的名称</returns>
        public string GetComputerName()
        {
            return (Dns.GetHostName());
        }

        /// <summary>
        /// 获取本机IP地址
        /// </summary>
        /// <returns>返回本地计算机的IP地址</returns>
        public string GetComputerIP()
        {
            string result = string.Empty;
            IPHostEntry addr = Dns.GetHostEntry(Dns.GetHostName());
            for (int i = 0; i < addr.AddressList.Length; i++)
            {
                if (addr.AddressList[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    result = addr.AddressList[i].ToString();
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// 获取本地MAC地址
        /// </summary>
        /// <returns></returns>
        public string GetComputerMAC()
        {
            ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
            using (ManagementObjectCollection mo = mAdapter.GetInstances())
            {
                foreach (ManagementBaseObject m in mo)
                {
                    if (Convert.ToBoolean(m["IPEnabled"]))
                    {
                        return m["MacAddress"].ToString();
                    }
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取操作系统版本
        /// </summary>
        /// <returns></returns>
        public string GetOSVersionString()
        {
            try
            {
                using (RegistryKey k = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion"))
                {
                    return (string.Format("{0} {1}", k.GetValue("ProductName"), k.GetValue("CSDVersion", string.Empty)));
                }
            }
            catch
            {
                return (Environment.OSVersion.VersionString);
            }
        }

        /// <summary>
        /// 设置本地时间
        /// </summary>
        /// <param name="d">要设置的时间</param>
        public void SetLocalTime(DateTime d)
        {
            SystemTime t = new SystemTime();
            t.wYear = Convert.ToUInt16(d.Year);
            t.wMonth = Convert.ToUInt16(d.Month);
            t.wDay = Convert.ToUInt16(d.Day);
            t.wHour = Convert.ToUInt16(d.Hour);
            t.wMinute = Convert.ToUInt16(d.Minute);
            t.wSecond = Convert.ToUInt16(d.Second);
            t.wMiliseconds = Convert.ToUInt16(d.Millisecond);
            SetLocalTime(ref t);
        }

        /// <summary>
        /// 获取本地时间
        /// </summary>
        /// <returns>返回本地时间</returns>
        public DateTime GetLocalTime()
        {
            SystemTime t = new SystemTime();
            GetLocalTime(ref t);
            return (new DateTime(t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond, t.wMiliseconds));
        }

        /*---------------------------------------------------
         * 扩展的对话框函数
         *---------------------------------------------------*/

        /// <summary>
        /// 显示错误窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        public void ErrorDlg(string Message)
        {
            ErrorDlg(Message, "错误");
        }

        /// <summary>
        /// 显示错误窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        public void ErrorDlg(string Message, string Caption)
        {
            DevExpress.XtraEditors.XtraMessageBox.Show(Message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 显示错误窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        public void ErrorDlg(IWin32Window owner, string Message)
        {
            ErrorDlg(owner, Message, "错误");
        }

        /// <summary>
        /// 显示错误窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        public void ErrorDlg(IWin32Window owner, string Message, string Caption)
        {
            DevExpress.XtraEditors.XtraMessageBox.Show(owner, Message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 显示提示窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        public void InfoDlg(string Message)
        {
            InfoDlg(Message, "信息");
        }

        /// <summary>
        /// 显示提示窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        public void InfoDlg(string Message, string Caption)
        {
            DevExpress.XtraEditors.XtraMessageBox.Show(Message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 显示提示窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        public void InfoDlg(IWin32Window owner, string Message)
        {
            InfoDlg(owner, Message, "信息");
        }

        /// <summary>
        /// 显示提示窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        public void InfoDlg(IWin32Window owner, string Message, string Caption)
        {
            DevExpress.XtraEditors.XtraMessageBox.Show(owner, Message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 显示提示确认窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        /// <returns>true表示确认；false表示取消</returns>
        public bool InfoOk(string Message)
        {
            return (InfoOk(Message, "信息"));
        }

        /// <summary>
        /// 显示提示确认窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        /// <returns>true表示确认；false表示取消</returns>
        public bool InfoOk(string Message, string Caption)
        {
            return (DevExpress.XtraEditors.XtraMessageBox.Show(Message, Caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.OK);
        }

        /// <summary>
        /// 显示提示确认窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        /// <returns>true表示确认；false表示取消</returns>
        public bool InfoOk(IWin32Window owner, string Message)
        {
            return (InfoOk(owner, Message, "信息"));
        }

        /// <summary>
        /// 显示提示确认窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        /// <returns>true表示确认；false表示取消</returns>
        public bool InfoOk(IWin32Window owner, string Message, string Caption)
        {
            return (DevExpress.XtraEditors.XtraMessageBox.Show(owner, Message, Caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.OK);
        }

        /// <summary>
        /// 显示警告窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        public void WarningDlg(string Message)
        {
            WarningDlg(Message, "警告");
        }

        /// <summary>
        /// 显示警告窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        public void WarningDlg(string Message, string Caption)
        {
            DevExpress.XtraEditors.XtraMessageBox.Show(Message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        /// <summary>
        /// 显示警告窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        public void WarningDlg(IWin32Window owner, string Message)
        {
            WarningDlg(owner, Message, "警告");
        }

        /// <summary>
        /// 显示警告窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        public void WarningDlg(IWin32Window owner, string Message, string Caption)
        {
            DevExpress.XtraEditors.XtraMessageBox.Show(owner, Message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        /// <summary>
        /// 显示询问窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        /// <returns>true表示是；false表示否</returns>
        public bool QueryDlg(string Message)
        {
            return (QueryDlg(Message, "询问"));
        }

        /// <summary>
        /// 显示询问窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        /// <returns>true表示是；false表示否</returns>
        public bool QueryDlg(string Message, string Caption)
        {
            return (DevExpress.XtraEditors.XtraMessageBox.Show(Message, Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes);
        }

        /// <summary>
        /// 显示询问窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        /// <returns>true表示是；false表示否</returns>
        public bool QueryDlg(IWin32Window owner, string Message)
        {
            return (QueryDlg(owner, Message, "询问"));
        }

        /// <summary>
        /// 显示询问窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        /// <returns>true表示是；false表示否</returns>
        public bool QueryDlg(IWin32Window owner, string Message, string Caption)
        {
            return (DevExpress.XtraEditors.XtraMessageBox.Show(owner, Message, Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes);
        }

        /// <summary>
        /// 显示重试窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        /// <returns>Abort表示终止；Retry表示重试；Ignore表示忽略</returns>
        public DialogResult RetryDlg(string Message)
        {
            return (RetryDlg(Message, "询问"));
        }

        /// <summary>
        /// 显示重试窗口
        /// </summary>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        /// <returns>Abort表示终止；Retry表示重试；Ignore表示忽略</returns>
        public DialogResult RetryDlg(string Message, string Caption)
        {
            return (DevExpress.XtraEditors.XtraMessageBox.Show(Message, Caption, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2));
        }

        /// <summary>
        /// 显示重试窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        /// <returns>Abort表示终止；Retry表示重试；Ignore表示忽略</returns>
        public DialogResult RetryDlg(IWin32Window owner, string Message)
        {
            return (RetryDlg(owner, Message, "询问"));
        }

        /// <summary>
        /// 显示重试窗口
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="Message">显示的文本</param>
        /// <param name="Caption">显示的标题</param>
        /// <returns>Abort表示终止；Retry表示重试；Ignore表示忽略</returns>
        public DialogResult RetryDlg(IWin32Window owner, string Message, string Caption)
        {
            return (DevExpress.XtraEditors.XtraMessageBox.Show(owner, Message, Caption, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2));
        }
        /// <summary>
        /// 显示异常对话框
        /// </summary>
        /// <param name="ex">捕捉到异常</param>
        public void ShowExceptionDlg(Exception ex)
        {
            (new frmError(ex)).ShowDialog();
        }

        /// <summary>
        /// 显示异常对话框
        /// </summary>
        /// <param name="owner">拥有模式对话框的窗体</param>
        /// <param name="ex">捕捉到异常</param>
        public void ShowExceptionDlg(IWin32Window owner, Exception ex)
        {
            (new frmError(ex)).ShowDialog(owner);
        }

        /// <summary>
        /// 显示MDI子窗体
        /// </summary>
        /// <typeparam name="T">子窗体</typeparam>
        /// <param name="owner">父窗体</param>
        public void ShowMdiForm<T>(DevExpress.XtraEditors.XtraForm owner) where T : DevExpress.XtraEditors.XtraForm, new()
        {
            using (DevExpress.Utils.WaitDialogForm waitdlg = new DevExpress.Utils.WaitDialogForm("正在加载...", "请稍候！"))
            {
                T dlg = new T();
                foreach (DevExpress.XtraEditors.XtraForm f in owner.MdiChildren)
                {
                    if (dlg.Name.Equals(f.Name))
                    {
                        f.Activate();
                        return;
                    }
                }
                dlg.MdiParent = owner;
                dlg.WindowState = FormWindowState.Maximized;
                dlg.Show();
            }
        }

        /// <summary>
        /// 显示MDI子窗体
        /// </summary>
        /// <typeparam name="T">子窗体</typeparam>
        /// <param name="owner">父窗体</param>
        /// <param name="dlg">声明的子窗体变量</param>
        public void ShowMdiForm<T>(DevExpress.XtraEditors.XtraForm owner, T dlg) where T : DevExpress.XtraEditors.XtraForm
        {
            foreach (DevExpress.XtraEditors.XtraForm f in owner.MdiChildren)
            {
                if (dlg.Name.Equals(f.Name))
                {
                    f.Activate();
                    return;
                }
            }
            dlg.MdiParent = owner;
            dlg.WindowState = FormWindowState.Maximized;
            dlg.Show();
        }
        /// <summary>
        /// 显示MDI子窗体
        /// </summary>
        /// <typeparam name="T">子窗体</typeparam>
        /// <param name="owner">父窗体</param>
        /// <param name="dlg">声明的子窗体变量</param>
        public void ShowMdiForm<T>(DevExpress.XtraEditors.XtraForm owner, T dlg, string key) where T : DevExpress.XtraEditors.XtraForm
        {
            foreach (DevExpress.XtraEditors.XtraForm f in owner.MdiChildren)
            {
                if (dlg.Name.Equals(f.Name) && string.Equals(f.Tag, key))
                {
                    f.Activate();
                    return;
                }
            }
            dlg.MdiParent = owner;
            dlg.WindowState = FormWindowState.Maximized;
            dlg.Show();
        }

        /// <summary>
        /// 打开对话框
        /// </summary>
        /// <param name="FileName">输出所打开的文件，完整的文件路径</param>
        /// <param name="Title">标题名称</param>
        /// <param name="Filter">文件名筛选器字符串</param>
        /// <param name="Ext">默认扩展名</param>
        /// <returns>确认打开返回true；否则返回false</returns>
        public bool OpenDialog(ref string FileName, string Title, string Filter, string Ext)
        {
            using (System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog())
            {
                dlg.FileName = FileName;
                dlg.DefaultExt = Ext;
                dlg.Filter = Filter;
                dlg.Title = Title;
                dlg.CheckFileExists = true;
                dlg.CheckPathExists = true;
                dlg.ValidateNames = true;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    FileName = dlg.FileName;
                    return (true);
                }
                return (false);
            }
        }

        /// <summary>
        /// 保存对话框
        /// </summary>
        /// <param name="FileName">输出所打开的文件，完整的文件路径</param>
        /// <param name="Title">标题名称</param>
        /// <param name="Filter">文件名筛选器字符串</param>
        /// <param name="Ext">默认扩展名</param>
        /// <returns>确认保存返回true；否则返回false</returns>
        public bool SaveDialog(ref string FileName, string Title, string Filter, string Ext)
        {
            using (System.Windows.Forms.SaveFileDialog dlg = new System.Windows.Forms.SaveFileDialog())
            {
                dlg.FileName = FileName;
                dlg.DefaultExt = Ext;
                dlg.Filter = Filter;
                dlg.CheckPathExists = true;
                dlg.ValidateNames = true;
                dlg.AddExtension = true;
                dlg.OverwritePrompt = true;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    FileName = dlg.FileName;
                    return (true);
                }
                return (false);
            }
        }

        /*---------------------------------------------------
         * 扩展的字符串操作函数
         *---------------------------------------------------*/

        /// <summary>
        /// 获取字符串实际长度
        /// </summary>
        /// <param name="S">字符串</param>
        /// <returns>返回实际长度</returns>
        public int StrLength(string S)
        {
            System.Text.ASCIIEncoding n = new System.Text.ASCIIEncoding();
            byte[] b = n.GetBytes(S);
            int length = 0;                          // length 为字符串的实际长度
            for (int i = 0; i <= b.Length - 1; i++)
            {
                if (b[i] == 63)             //判断是否为汉字或全脚符号
                {
                    length++;
                }
                length++;
            }
            return length;
        }

        /// <summary>
        /// 转换日期为 yyyy-MM 格式字符串
        /// </summary>
        /// <param name="Date">要转换日期</param>
        /// <returns>返回转换后的字符串</returns>
        public string MonthToStr(DateTime Date)
        {
            return (Date.ToString("yyyy-MM"));
        }

        /// <summary>
        /// 转换日期为 yyyy-MM-dd 格式字符串
        /// </summary>
        /// <param name="Date">要转换日期</param>
        /// <returns>返回转换后的字符串</returns>
        public string DateToStr(DateTime Date)
        {
            return (Date.ToString("yyyy-MM-dd"));
        }

        /// <summary>
        /// 转换时间为 HH:mm:ss 格式字符串
        /// </summary>
        /// <param name="Time">要转换时间</param>
        /// <returns>返回转换后的字符串</returns>
        public string TimeToStr(DateTime Time)
        {
            return (Time.ToString("HH:mm:ss"));
        }

        /// <summary>
        /// 转换日期时间为 yyyy-MM-dd HH:mm:ss 格式字符串
        /// </summary>
        /// <param name="ADateTime">要转换日期时间</param>
        /// <returns>返回转换后的字符串</returns>
        public string DateTimeToStr(DateTime ADateTime)
        {
            return (ADateTime.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        /// <summary>
        /// 校验字符串是否只包含字母与数字
        /// </summary>
        /// <param name="toVerified">需要校验的字符串</param>
        /// <returns>true表示符合要求，false表示不符合要求</returns>
        public bool IsOnlyLetterAndDigit(string toVerified)
        {
            Regex rx = new Regex(@"^[a-zA-Z0-9-]*$");
            return rx.IsMatch(toVerified.Trim(), 0);
        }

        /// <summary>
        /// 检验是否是整数
        /// </summary>
        /// <param name="str">需要检验的字符串</param>
        /// <returns>是否为整数：true是整数，false非整数</returns>
        public bool IsInt(string str)
        {
            Regex rx = new Regex(@"^[0123456789]+$");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 校验是否为正的浮点数
        /// </summary>
        /// <param name="price">需要检验的字符串</param>
        /// <returns>是否为正浮点，是返回true，否则返回false</returns>
        public bool IsFloat(string str)
        {
            Regex rx = new Regex(@"^[0-9]*(.)?[0-9]+$", RegexOptions.IgnoreCase);
            return rx.IsMatch(str.Trim());
        }

        /// <summary>
        /// 检验是否为数字
        /// </summary>
        /// <param name="str">需要检验的字符串</param>
        /// <returns>是否为数字：true代表是，false代表否</returns>
        public bool IsNumber(string str)
        {

            Regex rx = new Regex(@"^[+-]?[0123456789]*[.]?[0123456789]*$");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 检验字符串是否为日期时间
        /// </summary>
        /// <param name="str">需要检验的字符串</param>
        /// <returns>是否为日期时间：true代表是，false代表否</returns>
        public bool IsDateTime(string str)
        {
            Regex rx = new Regex(@"^[ ]*[012 ]?[0123456789]?[0123456789]{2}[ ]*[-]{1}[ ]*[01]?[0123456789]{1}[ ]*[-]{1}[ ]*[0123]?[0123456789]{1}[ ]*[012]?[0123456789]{1}[ ]*[:]{1}[ ]*[012345]?[0123456789]{1}[ ]*[:]{1}[ ]*[012345]?[0123456789]{1}[ ]*$");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 检验字符串是否为邮政编码
        /// </summary>
        /// <param name="str">需要检验的字符串</param>
        /// <returns>是否为邮政编码：true代表是，false代表否</returns>
        public bool IsPostCode(string str)
        {
            Regex rx = new Regex(@"^[0123456789]{6}$");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 检验字符串是否为身份证号
        /// </summary>
        /// <param name="str">需要检验的字符串</param>
        /// <returns>是否为身份证号：true代表是，false代表否</returns>
        public bool IsCode(string str)
        {
            Regex rx = new Regex(@"^[0123456789]{15,18}$");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 检验字符串是否为电子邮件
        /// </summary>
        /// <param name="str">需要检验的字符串</param>
        /// <returns>是否为电子邮件：true代表是，false代表否</returns>
        public bool IsEMail(string str)
        {
            Regex rx = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 检验字符串是否为中国地区的电话号码
        /// </summary>
        /// <param name="str">需要检验的字符串</param>
        /// <returns>是否为中国地区的电话号码：true代表是，false代表否</returns>
        public bool IsPhoneNumber(string str)
        {
            //Regex rx = new Regex(@"((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*");
            Regex rx = new Regex(@"(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}1[358][0-9]{9}$)");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 检验字符串是否为汉字
        /// </summary>
        /// <param name="str">需要检验的字符串</param>
        /// <returns>是否为汉字：true代表是，false代表否</returns>
        public bool IsChinese(string str)
        {
            Regex rx = new Regex(@"u4e00-u9fa5");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 检验字符串是否为双字节字符(包括汉字)
        /// </summary>
        /// <param name="str">需要检验的字符串</param>
        /// <returns>是否为双字节字符：true代表是，false代表否</returns>
        public bool IsDoubleByteChar(string str)
        {
            Regex rx = new Regex(@"[^x00-xff]");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 检验字符串是否为URL地址
        /// </summary>
        /// <param name="str">需要检验的字符串</param>
        /// <returns>是否为URL地址：true代表是，false代表否</returns>
        public bool IsURLAddress(string str)
        {
            Regex rx = new Regex(@"[a-zA-z]+://[^s]*");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 检验字符串是否为IP地址
        /// </summary>
        /// <param name="str">需要检验的字符串</param>
        /// <returns>是否为IP地址：true代表是，false代表否</returns>
        public bool IsIPAddress(string str)
        {
            Regex rx = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 清除字符串中的HTML标签(对于复杂的嵌套标签有时不准确)
        /// </summary>
        /// <param name="toEvaluate">指定的要被处理的字符串</param>
        /// <returns>清除HTML标签后的字符串</returns>
        public string RemoveHtmlTags(string toEvaluate)
        {
            Regex rx = new Regex(@"s/<[a-zA-Z/][^>]*>//g", RegexOptions.IgnoreCase);

            return rx.Replace(toEvaluate, "");
        }
    }

    /// <summary>
    /// 界面管理状态
    /// </summary>
    public enum ManageState
    {
        None,
        Append,
        AppendChild,
        Modify,
        Delete,
        Save,
        Cancel,
        Browse,
        Query,
        NotFound,
        Export,
        Import
    }

}
